<%@page import="com.googlecode.contests_repo.core.users.AccessRight"%>
<%@page
	import="com.googlecode.contests_repo.core.config.ConfigurationManager"%>
<%@page import="com.googlecode.contests_repo.core.users.UserManager"%>
<%@page import="com.googlecode.contests_repo.core.model.*"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.io.File"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Series</title>
</head>
<body>
	<%
		if (request.getParameter("submittedSeries") != null) {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/SeriesEditServlet");
			dispatcher.include(request, response);
		}

		String seriesId = request.getParameter("path"); //request.getParameter("series");
		String repoPath = ConfigurationManager.getInstance().getRepoRoot();
		String seriesPath = ConfigurationManager.getInstance()
				.getRepoRoot() + File.separator + seriesId;
		String seriesRelativePath = ConfigurationManager.getInstance()
				.getSeriesRelativePath();
		String publicFolder = "_public_files";
		String privateFolder = "_files";
		Series series = ResourcesUtils.getJSON(new File(seriesPath));
		String seriesTitle = series.getTitle();
		String loggedUser = (String) session.getAttribute("username");

		boolean canEdit = "1".equals(request.getParameter("edit"));
		String repoRoot = ConfigurationManager.getInstance().getRepoRoot();
		String resourcePath = repoRoot + File.separator + seriesId;
		boolean hasUserEditRights = UserManager.hasAccessRight(loggedUser,
				seriesPath, AccessRight.VIEW_FULL);
		String readOnly = (!canEdit || !hasUserEditRights) ? "readonly=\"readonly\""
				: "";
	%>
	<h1>Series Info</h1>

	<form method="post">
		<p>
			<font size="4"> <b>Id: </b> </font> <input type="text" name="id"
				readonly="readonly" value="<%=series.getId()%>">
		<p>
			<font size="4"> <b>Title: </b> </font> <input type="text"
				name="title" <%=readOnly%> value="<%=series.getTitle()%>"> <input
				type="hidden" name="old-title" value="<%=series.getTitle()%>">
		<p>
			<font size="4"> <b>About: </b> </font> <input type="text"
				name="about" <%=readOnly%> value="<%=series.getAbout()%>"> <input
				type="hidden" name="old-about" value="<%=series.getAbout()%>">
			<%
				if (hasUserEditRights) {
			%>
		
		<p>
			<font size="4"> <b>Notes: </b> </font> <input type="text"
				name="notes" <%=readOnly%> value="<%=series.getNotes()%>"> <input
				type="hidden" name="old-notes" value="<%=series.getNotes()%>">
			<%
				if (canEdit) {
			%>
			<br> <input type="submit" name="submittedSeries" value="Change">
			<a href="resource.jsp?path=<%=seriesId%>">Discard changes</a>
			<%
				}
				}
			%>
		
	</form>

	<h2>Contests:</h2>
	<table border=1>
		<tr>
			<th>Id</th>
			<th>Title</th>
			<th>About</th>
			<th>View</th>
			<%
				if (canEdit && hasUserEditRights) {
			%>
			<th>Edit</th>
			<th>Delete</th>
			<%
				}
			%>
		</tr>
		<%
			List<Contest> contestList = ResourcesUtils.fetchResources(new File(
					seriesPath));
			if (contestList != null) {
				for (Contest contest : contestList) {
					String contestDir = seriesPath + File.separator
							+ contest.getId();
					if (UserManager.hasAccessRight(loggedUser, contestDir,
							AccessRight.LIST)) {
		%>
		<tr>
			<td><%=contest.getId()%></td>
			<td><%=contest.getTitle()%></td>
			<td><%=contest.getAbout()%></td>
			<td><a
				href="resource.jsp?path=<%=seriesId + File.separator + contest.getId()%>">details</a>
			</td>
			<%
				}
						if (canEdit
								&& UserManager.hasAccessRight(loggedUser,
										contestDir, AccessRight.VIEW_FULL)) {
			%>
			<td><a
				href="resource.jsp?path=<%=String.format("%s%s%s", seriesId, File.separator,
								contest.getId())%>&edit=1">edit</a>
			</td>
			<td><a href="delete">delete</a></td>
			<%
				}
			%>

		</tr>
		<%
			}
			}
		%>
	</table>
	<br />
	<h2>Downloads:</h2>
			<table border=1>
				<tr>
					<th>Folder name</th>
					<th>Download</th>
					<th>Browse</th>
				</tr>
				<%
					if(new File(seriesPath + File.separator + publicFolder).exists()){
				%>
				<tr>
					<td><%=publicFolder%></td>
					<td><a
						href="downloadFolder.jsp?from=series&series=<%=seriesTitle%>&folder=<%=publicFolder%>">download</a>
					</td>
					<td><a
						href="view_folder.jsp?path=<%=URLEncoder.encode(seriesTitle + File.separator + publicFolder, "UTF-8")%>">browse</a>
					</td>
				</tr>
				<%
					}
				    if (new File(seriesPath + File.separator + privateFolder).exists()&&UserManager.hasAccessRight(loggedUser, seriesPath,
								    AccessRight.VIEW_FULL))
						    {
				%>
				<tr>
					<td><%=privateFolder%></td>
					<td><a
						href="downloadFolder.jsp?from=series&series=<%=seriesTitle%>&folder=<%=privateFolder%>">download</a>
					</td>
					<td>
						<a href="view_folder.jsp?path=<%=URLEncoder.encode(seriesTitle + File.separator + privateFolder, "UTF-8")%>">browse</a>
					</td>
				</tr>
				<%
						    }
				%>
				</table>
	<br />
<a href="seriesList.jsp">Back</a>
</body>
</html>